package com.ysyx.ysyx_imageserver.controller;

import com.ysyx.ysyx_imageserver.dto.BaseDto;
import com.ysyx.ysyx_imageserver.utils.OsUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @description: xxxx接口/类
 * @author:郭宇轩
 * @create:2023/4/18 11:41
 */
@RestController
public class ImgController {
    @Value("${windows.path}")
    private String dirPath;





    @PostMapping("/img")
    public BaseDto imgUpload(MultipartFile photo,String fileName, HttpServletRequest request){

//        String dirPath = request.getServletContext().getRealPath("img");
        System.out.println("=====");
        System.out.println(dirPath);

        File dir = new File(dirPath);
        if(!dir.exists()){
            dir.mkdir();
        }
        int result = 0;
        String time = null;
        try {
//            time = String.valueOf(System.currentTimeMillis());
//            System.err.println(time);
            File saveFile = new File(dirPath+File.separator+fileName+".png");
            photo.transferTo(saveFile);
            if(saveFile.length()>0){
                result = 1;
            }
//            photo.transferTo(new File(dirPath+File.separator+photo.getOriginalFilename()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new BaseDto(result,time,null);
    }

    @GetMapping("/img/{imgName}")
    public ResponseEntity<byte[]> imgDownload(@PathVariable String imgName, HttpServletRequest request){
//        ServletContext servletContext = request.getServletContext();

//        if(OsUtils.isWindows()){
//            System.err.println("win");
//            dirPath = winPath;
//        }
//        if(OsUtils.isLinux()){
//            dirPath = linuxPath;
//            System.err.println("linux");
//        }
        System.out.println("------------------------------------");
        String filePath = dirPath+File.separator+imgName+".png";

        System.err.println("=====");
        System.err.print(filePath);

        ResponseEntity<byte[]> responseEntity = null;

        try {
            InputStream is = new FileInputStream(filePath);
            byte[] data = new byte[is.available()];
            is.read(data);
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Disposition","attachment;filename="+imgName+".jpg");
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            HttpStatus statusCode = HttpStatus.OK;
            responseEntity = new ResponseEntity<byte[]>(data,headers,statusCode);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return responseEntity;
    }


}
